function prepare_log(d){
    function rep(i,v){
        $("#log_"+i).html(v);
    }
    rep('id',d['id']);
    rep('type',d['type']);
    rep('type_name',d['type_name']);
    rep('by',d['by_fullname']);
    if(d['by']!='0') $("#log_by").attr('href','users?uid='+d['by']);
    rep('to',d['to_fullname']);
    if(d['to']!='0') $("#log_to").attr('href','users?uid='+d['to']);
    rep('created',d['created']);
    rep('text',d['text']);
    rep('notes',d['notes']);
    $("#log_loading").hide();
    $("#log_body").show();
}
$(".flb").click(function(e){$(this).text()=='{ALL}'?$("#lt1 input").val('').keyup():$("#lt1 input").val($(this).text()).keyup()});
window.lRow = function(l){
    var lid = $(l).attr("lid");
    $("#dialog_log").dialog("open");
    $("#log_body").hide();
    $("#log_loading").show();
    window.ajax = $.get('ajax?r=GET_LOG&lid='+lid,function(data){
        if((data[0]['data'] === undefined) || ($.type(data) == "string")){
                $("#dialog_log").dialog("close");
                var data = new Array();
                data[0] = {'error': 500,'data': 'Internal Error, Please call Administrator!\n<!-- SystemErrorDetails: ' + window.data + '-->'};
            }
            $.each(data, function(index, value) {
                if(value['error']){
                    $("#dialog_log").dialog("close");
                    console.log("Error: " + value['error'] + "\nData: " + value['data'] + "\n");
                    insertStickyError(value['data'],value['error']);
                }else{
                    prepare_log(value['data']);
                }
            });
    });
}
$("a#eld").click(function(){
	$.fileDownload('ajax?r=full_log',{
        failCallback: function (data, url) {
        	if((data[0]['data'] === undefined) || ($.type(data) == "string")){
        		var data = new Array();
        		data[0] = {'error': 500,'data': 'Internal Error, Please call Administrator!\n<!-- SystemErrorDetails: ' + window.data + '-->'};
        	}
        	$.each(data, function(index, value) {
        		if(value['error']){
        			console.log("Error: " + value['error'] + "\nData: " + value['data'] + "\n");
        			insertStickyError(value['data'],value['error']);
        		}else{
        			window.formNeedSubmit = '';
        			insertAutoSuccess(value['data']);
        		}
			});
			$('html, body').animate({scrollTop: $("#ajax_result").offset().top});
        }});
    //.done(function () { alert('File download a success!'); })
    //.fail(function () { alert('File download failed!'); });
	//$.download('ajax.php','r=full_log');
	//window.open("./ajax.php?r=full_log","Download Log - STS","height=600,width=1000,location=no,resizable=no,scrollbars=no")
});